﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using Winter.BackEnd.Models.DataTypes;

namespace Winter.BackEnd.Models.Domain
{
    public partial class Student : IPerson
    {

        #region IPerson Members

        public string Role
        {
            get
            {
                return UserRoles.Student.RoleName;
            }
        }

        public string FullName
        {
            get
            {
                return Person.FirstName + " " + Person.SecondName + " " + Person.LastName;
            }
        }

        public string Sex
        {
            get
            {
                return Person.Sex.Caption;
            }
        }

        public string BirthDate
        {
            get
            {
                return this.AmIFriend ? Person.Birthdate.Value.ToShortDateString() : "";
            }
        }

        public string Email
        {
            get { return this.AmIFriend ? Person.Email : ""; }
        }

        public string FacultyName
        {
            get
            {
                return Group.Department.Faculty.Name;
            }
        }

        public string DepartmentName
        {
            get
            {
                return Group.Department.Name;
            }
        }

        public string GroupName
        {
            get
            {
                return Group.Name;
            }
        }

        public IDictionary<string, object> Duties
        {
            get
            {
                var duties = new Dictionary<string, object>();
                ///TODO: replace
                duties.Add("facultyName", FacultyName);
                duties.Add("departmentName", DepartmentName);
                duties.Add("groupName", GroupName);

                return duties;
            }
        }

        public IList<MessageDTO> IncomeMessages
        {
            get
            {
                var messages = new List<MessageDTO>();
                Array.ForEach<PersonMessage>(Person.PersonMessages.Where(pm => pm.PersonID == Person.ID &&
                   !pm.Message.IsDeleted).OrderByDescending(pm => pm.Message.SendTime).ToArray(), (incomeMessage) =>
                {
                    messages.Add(new MessageDTO()
                    {
                        Id = incomeMessage.Message.ID,
                        SenderID = incomeMessage.Message.Sender.ID.ToString(),
                        MessageBody = incomeMessage.Message.Body,
                        Sender = incomeMessage.Message.Sender.FullName,
                        Subject = string.IsNullOrEmpty(incomeMessage.Message.Subject) ? Constants.NoSubject : incomeMessage.Message.Subject,
                        SenderPhoto = incomeMessage.Message.Sender.Image,
                        Time = incomeMessage.Message.SendTime.ToString("HH:mm dd.MM.yyyy"),
                        SenderName = this.Person.UserName,
                        IsNew = incomeMessage.Message.IsNew
                    });
                });


                return messages;
            }
        }

        public IList<Winter.BackEnd.Models.DataTypes.MessageDTO> SentMessages
        {
            get
            {
                List<MessageDTO> messages = new List<MessageDTO>();


                Array.ForEach<Message>(Repository.CurrentUser.Person.Messages.OrderByDescending(m => m.SendTime).ToArray(), (sentMessage) =>
                    {
                        messages.Add(new Winter.BackEnd.Models.DataTypes.MessageDTO()
                        {
                            Id = sentMessage.ID,
                            MessageBody = sentMessage.Body,
                            Sender = sentMessage.Sender.FullName,
                            Subject = string.IsNullOrEmpty(sentMessage.Subject) ?
                                            Constants.NoSubject : sentMessage.Subject,
                            SenderPhoto = sentMessage.Sender.Image,
                            Time = sentMessage.SendTime.ToString("HH:mm dd.MM.yyyy"),
                            SenderName = this.Person.UserName,
                            IsNew = false
                        });
                    });

                return messages;
            }
        }
        public bool AmIFriend
        {
            get
            {
                return this.Person.PersonFriends.Any(pf => pf.FriendID == Repository.CurrentUser.Person.ID) ||
                Repository.CurrentUser.Person.ID == this.Person.ID;
            }
        }
        public bool IsFriend
        {
            get
            {
                return Repository.CurrentUser.Person.PersonFriends.Any(pf => pf.FriendID == this.Person.ID) ||
                Repository.CurrentUser.Person.ID == this.Person.ID;
            }
        }
        public IEnumerable<MenuLink> NavigationMenu
        {
            get
            {
                List<MenuLink> menu = new List<MenuLink>();
                menu.Add(new MenuLink()
                {
                    LinkText = "Профайл",
                    ActionName = "Index",
                    ControllerName = "Person"
                });
                menu.Add(new MenuLink()
                {
                    LinkText = "Розклад",
                    ActionName = "Schedule",
                    ControllerName = "Group"
                });
                menu.Add(new MenuLink()
                {
                    LinkText = "Група",
                    ActionName = "Index",
                    ControllerName = "Group"
                });
                menu.Add(new MenuLink()
                {
                    LinkText = "Навчальний план",
                    ActionName = "Index",
                    ControllerName = "Subjects"
                });
                menu.Add(new MenuLink()
                {
                    LinkText = "Повідомлення",
                    ActionName = "Index",
                    ControllerName = "Message"
                });

                return menu;
            }
        }

        public static bool IsStudent(Person person)
        {
            return person.Student != null;
        }
        public string Image
        {
            get
            {
                return Person.Image;
            }
        }
        #endregion

    }
}
